package com.fatloss.angel;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;


public class DBclass extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 3;
	private static final String DATABASE_NAME = "DB_APPLE1";
    public static final String TABLE_NAME_CALORIE = "calorie_rec";
    private static final String CALORIE_TABLE_CREATE =
                "CREATE TABLE " + TABLE_NAME_CALORIE + " (" +
                "_id INTEGER PRIMARY KEY," +
                "food TEXT, " +
                "time TEXT, " +
                "calories INTEGER" +
                ");";
    private SQLiteDatabase dbWrite;
    private SQLiteDatabase dbRead;
    private Context m_context;
        
    DBclass(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        m_context = context;
        dbRead = this.getReadableDatabase();
        dbWrite = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) { 	
        db.execSQL(CALORIE_TABLE_CREATE);    
        Toast.makeText(m_context, "create db", Toast.LENGTH_SHORT).show();
    }

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_CALORIE);
		db.execSQL(CALORIE_TABLE_CREATE);
		Toast.makeText(m_context, "update db...old:"+String.valueOf(arg1)+" new: "+String.valueOf(arg2), Toast.LENGTH_LONG).show();
	}
	
	public int getOne()
	{
		
		Cursor cs = dbRead.query(TABLE_NAME_CALORIE, null, null, null, null, null, null);
		if ( cs.getCount() == 0)
			return 0;
		cs.moveToLast();
		return cs.getInt(cs.getColumnIndex("calories"));
	}
	
	public void insertOne(int cal, String food, String timestr)
	{
		ContentValues cv = new ContentValues();
		cv.put("food", food);
		cv.put("calories", cal);
		cv.put("time", timestr);
		dbWrite.insert(TABLE_NAME_CALORIE, null, cv);
		
	}
	
}
